package sshstudy.user.service;

import java.util.ArrayList;
import java.util.List;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import sshstudy.user.model.User;

@Scope("prototype")
@Component
public class UserServiceImpl implements UserService {

	private static List<User> users;
	
	static {
		users = new ArrayList<User>();
		users.add(new User("0", "Admin", "Admin", "---"));
		long id = System.currentTimeMillis();
		for (int i = 1; i <= 10; i++) {
			users.add(new User("" + (id + i), "user" + i, "password" + i, "---"));
		}
	}
	
	public List<User> list() {
		return users;
	}

	public User find(String id) {
		for (int i = 0; i < users.size(); i++) {
			User user = (User)users.get(i);
			if(id.equals(user.getId())) {
				return user;
			} 
		}
		return null;
	}
		
	public void add(User user) {
		users.add(user);
	}

	public void update(User user) {
		for (int i = 0; i < users.size(); i++) {
			User u = (User)users.get(i);
			if(user.getId().equals(u.getId())) {
				u.setUsername(user.getUsername());
				u.setPassword(user.getPassword());
				u.setDescription(user.getDescription());
				return;
			}
		}
	}

	public void delete(User user) {
		users.remove(user);
	}
}
